using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class StatementDelimiterProductionSpecification {
        [Test]
        public void ShouldParseSemiColon() {
            var source = " ;";

            var result = StatementDelimiterProduction.CreateTerminal ().Parse (source, 0, new ParseContext () {BracketDepth = 1});

            Assert.IsNotNull (result);
            Assert.AreEqual (";", ((KeywordTerm) result.Term).Name);
            Assert.AreEqual (1, result.Context.BracketDepth);
            Assert.AreEqual (2, result.Index);
            Assert.AreEqual (new SourceInformation (null, 1, 1), result.Term.SourceInformation);
        }

        [Test]
        public void ShouldParseNewLine() {
            var source = " \n    id";

            var result = StatementDelimiterProduction.CreateTerminal ().Parse (source, 0, new ParseContext () {LastIndent = "    "});

            Assert.IsNotNull (result);
            Assert.AreEqual (";", ((KeywordTerm) result.Term).Name);
            Assert.AreEqual ("    ", result.Context.LastIndent);
            Assert.AreEqual (6, result.Index);
            Assert.AreEqual (new SourceInformation (null, 1, 5), result.Term.SourceInformation);
        }

        [Test]
        public void ShouldParseNewLineIfBracketDepthIsGreaterThanZero() {
            var source = " \n    id";

            var result = StatementDelimiterProduction.CreateTerminal ().Parse (source, 0, new ParseContext () {BracketDepth = 1, LastIndent = "    "});

            Assert.IsNotNull (result);
            Assert.AreEqual (";", ((KeywordTerm) result.Term).Name);
            Assert.AreEqual ("    ", result.Context.LastIndent);
            Assert.AreEqual (6, result.Index);
            Assert.AreEqual (new SourceInformation (null, 1, 5), result.Term.SourceInformation);
        }

        [Test]
        public void ShouldParseIndentedNewLineIfBracketDepthIsGreaterThanZero() {
            var source = " \n    id";

            var result = StatementDelimiterProduction.CreateTerminal ().Parse (source, 0, new ParseContext () {BracketDepth = 1, LastIndent = ""});

            Assert.IsNotNull (result);
            Assert.AreEqual (";", ((KeywordTerm) result.Term).Name);
            Assert.AreEqual ("    ", result.Context.LastIndent);
            Assert.AreEqual (6, result.Index);
            Assert.AreEqual (new SourceInformation (null, 1, 5), result.Term.SourceInformation);
        }

        [Test]
        public void ShouldParseUnindentedNewLineIfBracketDepthIsGreaterThanZero() {
            var source = " \nid";

            var result = StatementDelimiterProduction.CreateTerminal ().Parse (source, 0, new ParseContext () {BracketDepth = 1, LastIndent = "    "});

            Assert.IsNotNull (result);
            Assert.AreEqual (";", ((KeywordTerm) result.Term).Name);
            Assert.AreEqual ("", result.Context.LastIndent);
            Assert.AreEqual (2, result.Index);
            Assert.AreEqual (new SourceInformation (null, 1, 1), result.Term.SourceInformation);
        }

        [Test]
        public void ShouldNotParseAnythingElse() {
            var source = " id";

            var result = StatementDelimiterProduction.CreateTerminal ().Parse (source, 0, new ParseContext ());

            Assert.IsNull (result);
        }
    }
}